package com.easypreview.preview.controller.cms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easypreview.preview.util.page.Page;
import com.easypreview.preview.util.web.exception.MaliciousRequestException;
import com.easypreview.preview.util.web.exception.ResourceNotFoundException;
import com.easypreview.preview.service.channel.ChannelService;
import com.easypreview.preview.domain.channel.Channel;
import com.easypreview.preview.util.web.exception.BadRequestException;
import com.google.appengine.api.datastore.KeyFactory;

@Controller
public class ChannelController {

	@Autowired
	private ChannelService channelService;
	
	/**
	 * 频道列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/channel/list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("channels",channelService.queryChannels());
		return "channel/list";
	}
	
	/**
	 * 生成添加频道页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/channel/add", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute(new Channel());
		return "channel/add";
	}
	
	/**
	 * 提交频道信息
	 * @param channel
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/channel/add", method = RequestMethod.POST)
	public String create(Channel channel, BindingResult result,HttpServletRequest request) {
		if (result.hasErrors()) {
			throw new MaliciousRequestException("Binding fail!!!");
		}		
		try{
			channelService.save(channel);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "redirect:/channel/list";
	}
	
	/**
	 * 删除频道
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/channel/{id}/delete", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") String id) {
		try{
			channelService.delete(id);
		}
		catch(Exception exception)
		{
			exception.printStackTrace();
			return null;
		}
		return "redirect:/channel/list";
	}
//
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see com.youboy.core.web.controller.CrudControllerSupport#delete(javax.servlet.http.HttpServletRequest)
//	 */
//	@Override
//	@RequestMapping(value = "/channel/delete", method = RequestMethod.DELETE)
//	public String delete(HttpServletRequest request) {
//		String[] items = EntityUtils.nullSafe(request.getParameterValues("items"), new String[] {});
//
//		for (String item : items) {
//			delete(item);
//		}
//
//		return REDIRECT_LIST;
//	}
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see com.youboy.core.web.controller.CrudControllerSupport#edit(java.lang.Object, org.springframework.ui.Model)
//	 */
//	@Override
//	@RequestMapping(value = "/channel/{id}/edit", method = RequestMethod.GET)
//	public String edit(@PathVariable("id") String id, Model model) {
//		Channel channel = channelService.get(id);
//		model.addAttribute(channel)
//			 .addAttribute("child", channelService.queryChildById(id))
//			 .addAttribute("channelNames", channelService.queryChannels())
//			 .addAttribute("template", templateService.queryTemplates());
//		return formView();
//	}
//
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see com.youboy.core.web.controller.CrudControllerSupport#edit(java.lang.Object,
//	 * javax.servlet.http.HttpServletRequest)
//	 */
//	@RequestMapping(value = "/channel/{id}/edit", method = RequestMethod.PUT)
//	public String edit(@PathVariable("id") String id, HttpServletRequest request) {
//		try {
//			Channel channel = channelService.get(id);
//			channel.setFather(channelService.get(request.getParameter("belongChannel")));
//			channel.setTemplate(templateService.get(request.getParameter("belongTemplate")));
//			bind(request, channel);
//			channelService.update(channel);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
//
//		return REDIRECT_LIST;
//	}
//	@RequestMapping(value = "/channel/centre", method = RequestMethod.GET)
//	public String centre() {
//		return "channel/centre";
//	}
//	@RequestMapping(value = "/channel/left", method = RequestMethod.GET)
//	public String left(Model model) {
//		return "channel/left";
//	}
	
}
